package com.xjrsoft.module.customer.erp_model.eRP_Turnover.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.dto.ErpStockinventoryListDto;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.dto.UpdateInventorDtoList;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.dto.UpdateInventoryDto;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.entity.ErpStockinventory;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.entity.ErpStockinventoryentry;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.mapper.ErpStockinventoryMapper;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.mapper.ErpStockinventoryentryMapper;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.service.IErpStockinventoryService;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.service.IErpStockinventoryentryService;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.vo.ErpStockinventoryListVo;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.vo.ErpStockinventoryVo;
import com.xjrsoft.module.customer.erp_model.erp_inventoryManage.entity.ErpInventory;
import com.xjrsoft.module.customer.erp_model.erp_inventoryManage.service.IErpInventoryService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-11-10
 */
@Service
@AllArgsConstructor
public class ErpStockinventoryServiceImpl extends BaseService<ErpStockinventoryMapper, ErpStockinventory> implements IErpStockinventoryService {

	private ErpStockinventoryentryMapper erpStockinventoryentryMapper;
	private ErpStockinventoryMapper erpStockinventoryMapper;

	private IErpInventoryService erpInventoryService;

	private IErpStockinventoryentryService erpStockinventoryentryService;

	@Override
	public IPage<ErpStockinventory> getPageList(ErpStockinventoryListDto pageListDto) {
		Wrapper<ErpStockinventory> wrapper = Wrappers.<ErpStockinventory>query().lambda()				.eq(!StringUtil.isEmpty(pageListDto.getF_InventoryCode()), ErpStockinventory::getFInventorycode, pageListDto.getF_InventoryCode())
				.ge(!StringUtil.isEmpty(pageListDto.getF_InventoryDate_Start()), ErpStockinventory::getFInventorydate, pageListDto.getF_InventoryDate_Start())
				.le(!StringUtil.isEmpty(pageListDto.getF_InventoryDate_End()), ErpStockinventory::getFInventorydate, pageListDto.getF_InventoryDate_End())
				.eq(!StringUtil.isEmpty(pageListDto.getF_WareHouseId()), ErpStockinventory::getFWarehouseid, pageListDto.getF_WareHouseId())
				.like(!StringUtil.isEmpty(pageListDto.getF_AuditMark()), ErpStockinventory::getFAuditmark, pageListDto.getF_AuditMark());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}
	public IPage<ErpStockinventoryListVo> getListInfo(ErpStockinventoryListDto pageListDto) {
		IPage<ErpStockinventoryListVo> page = ConventPage.getPage(pageListDto);
		String getF_InventoryCode=pageListDto.getF_InventoryCode();
		if (!StringUtil.isEmpty(getF_InventoryCode)) {
			getF_InventoryCode = StringPool.PERCENT + getF_InventoryCode + StringPool.PERCENT;
		}
		List<ErpStockinventoryListVo> listInfo = erpStockinventoryMapper.getListInfo(getF_InventoryCode, pageListDto.getF_InventoryDate_Start(), pageListDto.getF_InventoryDate_End(), pageListDto.getF_WareHouseId(), pageListDto.getF_AuditMark(), page);
		page.setRecords(listInfo);
		return page;
		//return this.page(ConventPage.getPage(pageListDto), erpStockinventoryMapper.getList(pageListDto.getF_InventoryCode(),pageListDto.getF_InventoryDate_Start(),pageListDto.getF_InventoryDate_End(),pageListDto.getF_WareHouseId(),pageListDto.getF_AuditMark()));;

	}
	@Override
	public String addErpStockinventory(ErpStockinventory erpStockinventory, List<ErpStockinventoryentry> erpStockinventoryentryList) {
		String erpStockinventoryId = IdWorker.get32UUID();
		erpStockinventory.setFId(erpStockinventoryId);
		erpStockinventoryentryList.forEach(erpStockinventoryentry -> erpStockinventoryentry.setFInventoryid(erpStockinventoryId));
		this.saveBatch(erpStockinventoryentryList, ErpStockinventoryentry.class, ErpStockinventoryentryMapper.class);
		boolean isSuccess = this.save(erpStockinventory);
		return isSuccess ? erpStockinventoryId : null;
	}

	public String updateInventory(List<UpdateInventoryDto> updateInventoryDtos)
	{
		for (int i=0;i<updateInventoryDtos.size();i++)
		{
			ErpInventory erpInventory=new ErpInventory();
			erpInventory.setFId(updateInventoryDtos.get(i).getFStockid());
			erpInventory.setFBasicamount(updateInventoryDtos.get(i).getFBasicamount().add(updateInventoryDtos.get(i).getFBasicdiff()));
			erpInventory.setFComamount(updateInventoryDtos.get(i).getFComamount().add(updateInventoryDtos.get(i).getFComdiff()));
			erpInventoryService.updateById(erpInventory);
			ErpStockinventoryentry erpStockinventoryentry=new ErpStockinventoryentry();
			erpStockinventoryentry.setFId(updateInventoryDtos.get(i).getFInventoryid());
			erpStockinventoryentry.setFAuditmark(2);
			erpStockinventoryentryService.updateById(erpStockinventoryentry);
		}
		return null;
	}

	@Override
	public boolean updateErpStockinventory(String id, ErpStockinventory erpStockinventory, List<ErpStockinventoryentry> erpStockinventoryentryList) {
		erpStockinventory.setFId(id);
		erpStockinventoryentryList.forEach(erpStockinventoryentry -> erpStockinventoryentry.setFInventoryid(id));
		erpStockinventoryentryMapper.delete(Wrappers.<ErpStockinventoryentry>query().lambda().eq(ErpStockinventoryentry::getFInventoryid, id));
		this.saveBatch(erpStockinventoryentryList, ErpStockinventoryentry.class, ErpStockinventoryentryMapper.class);
		return this.updateById(erpStockinventory);
	}

	public List<ErpStockinventoryentry> getErpStockinventoryentryByParentId(String parentId){
		Wrapper wrapper = Wrappers.<ErpStockinventoryentry>query().lambda().eq(ErpStockinventoryentry::getFInventoryid, parentId);
		return erpStockinventoryentryMapper.selectList(wrapper);
	}
}
